function formateDate(date) {
  date = new Date(date);
  return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
}
// 从浏览器的地址栏中获取查询参数
function getUrlParams(name) {
  var paramsAry = location.search.substr(1).split('&');
  // 循环数据
  for (var i = 0; i < paramsAry.length; i++) {
    var tmp = paramsAry[i].split('=');
    if (tmp[0] == name) {
      return tmp[1];
    }
  }
  return -1;
}
// 随机推荐
$.ajax({
  method: 'get',
  url: 'http://localhost:3000/posts/random',
  success: function (res) {
    // console.log(res);
    var randomTpl = `
        {{each data}}
        <li>
        <a href="detail.html?id={{$value._id}}">
          <p class="title">{{$value.title}}</p>
          <p class="reading">阅读({{$value.meta.views}})</p>
          <div class="pic">
            <img src="{{$value.thumbnail}}" alt="">
          </div>
        </a>
      </li>  
        {{/each}}`;
    var html = template.render(randomTpl, {
      data: res
    });
    $('#randomBox').html(html)
  }
})
//获取最新评论
$.ajax({
  method: 'get',
  url: 'http://localhost:3000/comments/lasted',
  success: function (res) {
    // console.log(res);
    var commentTpl = `
        {{each data}}
        <li>
        <a href="javascript:;">
          <div class="avatar">
            <img src="{{$value.author.avatar}}" alt="">
          </div>
          <div class="txt">
            <p>
              <span>{{$value.author.nickName}}</span>{{$imports.formateDate($value.createAt)}}说:
            </p>
            <p>{{$value.content}}</p>
          </div>
        </a>
      </li>
        {{/each}}
        `;
    var html = template.render(commentTpl, {
      data: res
    });
    // console.log(html);
    $('#commentBox').html(html)
  }
})
// 侧边导航
$.ajax({
  type: 'get',
  url: 'http://localhost:3000/categories',
  success: function (res) {
    var navTpl = `
			{{each data}}
			<li>
				<a href="list.html?categoryId={{$value._id}}">
					<i class="fa {{$value.className}}"></i>{{$value.title}}
				</a>
			</li>
			{{/each}}
		`;
    var html = template.render(navTpl, {
      data: res
    });
    $('#navBox').html(html)
    $('#topNavBox').html(html)
  }
})
// 获取到搜索表单 并为其添加表单提交事件
// $('.search form').on('submit', function () {
//     // 获取到用户在表单中输入的搜索关键字
//     var keys = $(this).find('.keys').val();
//     // 跳转到搜索结果页面 并且将用户输入的搜索关键字传递到搜索结果页面
//     location.href = "/search.html?key=" + keys
//     // 阻止表单默认提交行为
//     return false;
// });